package a09_贪心算法;

import java.util.Arrays;

/**
 * <p>
 * a02_分发饼干复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/1/8
 */
public class a02_分发饼干复习1 {

    public static void main(String[] args) {
        int[] children = new int[]{10,9,8,7};
        int[] foods = new int[]{5,6,7,8};
        a02_分发饼干复习1 test = new a02_分发饼干复习1();
        int contentChildren = test.findContentChildren(children, foods);
    }
    public int findContentChildren(int[] children, int[] foods) {
        int result = 0;
        if (foods.length == 0) {
            return result;
        }
        Arrays.sort(children);
        Arrays.sort(foods);

        //遍历每个学生
        int foodIdx = foods.length - 1;
        for (int childIdx = children.length - 1; childIdx >= 0; childIdx--) {
            if(children[childIdx] <= foods[foodIdx]){//够吃
                result++;
                foodIdx--;//食物吃过了就不能使用了
                if(foodIdx < 0){//食物吃完了
                    break;
                }
            }
        }
        return result;
    }
}
